package com.woniu51.user.controller;

import com.woniu.common.core.domain.AjaxResult;
import com.woniu51.dto.RegisterDto;
import com.woniu51.entity.User;
import com.woniu51.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashSet;
import java.util.Set;

/**
 * @author 杨光
 * @date 2022年03月29日 11:13
 */
@RestController
public class UserController {


    @Autowired
    private UserService userService;

    //注册用户
    @RequestMapping("/web/register")
    public AjaxResult register(@RequestBody RegisterDto registerDto){
        Integer result = userService.register(registerDto);

        return AjaxResult.success("ok",result);
    }


    //检查手机号码是否被占用
    @RequestMapping("/web/checkCellphoneExist")
    public AjaxResult checkCellphoneExist(String cellPhone){
        Integer result = userService.checkCellphoneExist(cellPhone);

        return AjaxResult.success("ok",result);
    }
    //申请手机验证码
    @RequestMapping("/web/queryVerifyCode")
    public AjaxResult queryVerifyCode(String cellPhone){
        Integer result = userService.queryVerifyCode(cellPhone);

        return AjaxResult.success("ok",result);
    }

    @RequestMapping("/feign/queryUserByUserAccountAndPassword")
    public User queryUserByUserAccountAndPassword(@RequestParam("userAccount") String userAccount,
                                                  @RequestParam("userPassword") String userPassword){
        User user = userService.queryUserByUserAccountAndPassword(userAccount, userPassword);

        return user;
    }



    @RequestMapping("getInfo")
    public AjaxResult getInfo(HttpServletRequest request){
        AjaxResult ajax = AjaxResult.success();

//        String userIdStr = request.getHeader("userid");//这个请求头是网关添加的
//        if (userIdStr != null) {
//            Integer userId = Integer.valueOf(userIdStr);
        //由于目前只有一个admin用户，这里就不查询数据库了，直接返回超级用户的权限
        Set<String> roles =  new HashSet<>();
        roles.add("admin");
        Set<String> perms = new HashSet<String>();
        perms.add("*:*:*");
        User user = new User();
        user.setUserName("admin");
        user.setUserAccount("admin");
        user.setUserId(1L);
        ajax.put("user",user );
        ajax.put("roles", roles);
        ajax.put("permissions", perms);
//        }
        return ajax;
    }

}
